define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {


    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'outhouse/rdinrecord/index' + location.search,
                    add_url: 'outhouse/rdinrecord/add',
                    edit_url: 'outhouse/rdinrecord/edit',
                    del_url: 'outhouse/rdinrecord/del',
                    multi_url: 'outhouse/rdinrecord/multi',
                    //import_url: 'outhouse/rdinrecord/import',
                    table: 'rdinrecord',
                }
            });
            Fast.config.openArea = ['100%','100%'];

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                    //    {field: 'id', title: __('Id')},
                        {field: 'code', title:'单号', operate: 'LIKE',formatter:function(val,row,i){
                           return row.rdinrecord.code;
                        }},
                        {field: 'code_date', title: '单号日期', operate:'RANGE', addclass:'datetimerange', autocomplete:false,formatter:function(val,row,i){
                            return row.rdinrecord.code_date;
                        }},
                        {field: 'cInvCode', title: '物料编码', operate: 'LIKE'},
                        {field: 'iQuantity', title: '数量', operate:'BETWEEN'},
                        {field: 'warehouse_id', title:'仓库',formatter:function(val,row,i){
                                return row.rdinrecord.ware_house.name;
                            }},

                        {field: 'shelves_id', title: '货架/货区',formatter:function(val,row,i){
                                return row.shelves.name;
                        }},
                        {field: 'check_status', title: '锁单状态', searchList: {"0":'待锁',"1":'已锁'}, formatter:function(varl,row,i){
                            if(row.rdinrecord.check_status==1){
                                return '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="" data-field="check_status" data-value="1" data-original-title="点击搜索 已锁单"><span class="text-success">已锁单</span></a>';
                            }else{
                                return '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="" data-field="check_status" data-value="0" data-original-title="点击搜索 待锁单"><span class="text-primary">待锁单</span></a>';
                            }
                        }},
                        {field: 'status', title: '处理状态', searchList: {"0":'待处理',"1":'已处理'}, formatter:function(varl,row,i){
                                if(row.rdinrecord.status==1){
                                    return '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="" data-field="status" data-value="1" data-original-title="点击搜索 已处理"><span class="text-success">已处理</span></a>';
                                }else{
                                    return '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="" data-field="status" data-value="0" data-original-title="点击搜索 待处理"><span class="text-primary">待处理</span></a>';
                                }
                            }},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                       // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        /*{field: 'operate', title: __('Operate'), table: table,  formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'check',
                                    text: '锁单',
                                    title: '锁单',
                                    icon: 'fa fa-magic',
                                    confirm: '确定锁单',
                                    classname: 'btn btn-xs btn-info btn-view btn-ajax',
                                    url: 'outhouse/rdinrecord/checkStatus?value=1',
                                    visible: function (row) {
                                        if(row.rdinrecord.check_status==0){
                                            return true;
                                        }
                                        //返回true时按钮显示,返回false隐藏
                                        return false;
                                    },
                                    success: function (data, ret) {
                                        // Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                        table.bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        // console.log(data, ret);
                                        Backend.api.toastr.error(ret.msg);
                                        return false;
                                    }
                                  },
                                  {
                                      name: 'check',
                                      text: '反锁',
                                      title: '反锁',
                                      icon: 'fa fa-magic',
                                      confirm: '确定反锁',
                                      classname: 'btn btn-xs btn-info btn-view btn-ajax',
                                      url: 'outhouse/rdinrecord/checkStatus?value=0',
                                      visible: function (row) {
                                          if(row.rdinrecord.check_status==1){
                                              return true;
                                          }
                                          //返回true时按钮显示,返回false隐藏
                                          return false;
                                      },
                                      success: function (data, ret) {
                                          // Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                          //如果需要阻止成功提示，则必须使用return false;
                                          //return false;
                                          table.bootstrapTable('refresh');
                                      },
                                      error: function (data, ret) {
                                          // console.log(data, ret);
                                          Backend.api.toastr.error(ret.msg);
                                          return false;
                                      }
                                    }],
                            events: Table.api.events.operate
                        }*/
                    ]
                ]
            });

            
            //处理单据操作
            $(document).on("click", ".btn-danger", function (e, obj) {
                let index = layer.confirm('确定要处理单据，此操作不可逆转？', {
                	  btn: ['确定','关闭'], //按钮
                	  title:'温馨提示'
                }, function(index){
                      layer.close(index);
                      let ids = Table.api.selectedids(table);
                       
                      //隐藏审核按钮，免得再次点击，重复提交
                      //$(".btn-danger").addClass('hidden')
                      Fast.api.ajax({
                          url: 'outhouse/rdinrecord/dealStatus',
                          data: {ids:ids}
	                      }, function (data) {
	                    	  $("a.btn-refresh").trigger("click");
	                      });
	                	}, function(){
	                		//$("a.btn-refresh").trigger("click");
	                });
           });
            
            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {

            Controller.api.bindevent();
        },
        edit: function () {

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                //Form.api.bindevent($("form[role=form]"));
            	Form.api.bindevent($("form[role=form]"),function(data,ret){
            		Toastr.success("操作成功");
            		//关闭新增，打开编辑
            		if(data!=''){
	            		var data = JSON.parse(data);
	            		if(data.action=='add'){
	            			Fast.api.close(); // 关闭弹窗
	            			//parent.Backend.api.open('outhouse/rdinrecord/edit?ids='+data.rent_id,"编辑寄仓入库单",{area:["100%", "100%"]}) ;
	            		}else if(data.action=='edit'){
	            			$("a.btn-refresh").trigger("click");
	            		}
            		}
            		parent.$("a.btn-refresh").trigger("click");
            		return false;
            	},function(data,ret){
            		Toastr.error(ret.msg);
            		console.log(2)
            		return false;
            	},function(success,error){
            		Form.api.submit($("form[role=form]"), success, error);
            		return false;
            	});
            }
        }
    };
    return Controller;
});
function selectshelves(obj){
    let house_id = $("#c-warehouse_id").val();
    let this_obj = $(obj);
    let this_id_obj = this_obj.siblings('.c-shelves_id');
    let this_name_obj = this_obj.siblings('.c-shelves_name');
    top.Fast.api.open('information/shelves/choose?warehouse_id='+house_id,'选择货架',{offset: 'r',callback:function(value){
            // $("#c-shelves_id").val(value);
            this_id_obj.attr('value',value.id);
            this_name_obj.val(value.name);
            this_name_obj.selectPageRefresh();
        }});
}
$('#choose-goods').on('click',function(){
    let house_id = $("#c-warehouse_id").val();
    if(!house_id){
        Backend.api.toastr.error('请先选择仓库');
        return false;
    }
    top.Fast.api.open('information/Inventory/choosebyclass','选择物料',{offset: 'r',callback:function(value){
            //console.log(value);
            $("#c-basic-textarea").val(JSON.stringify(value));
            $("#free-programme-item-table").trigger("fa.event.refreshfieldlist");

        }});
})
